package service;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;


public class Test {
    private static Connection connection;

    /**
     * 读入excel文件，解析后返回
     *
     * @throws IOException
     */
    public static void main(String[] arg0) {
        init();

        //获得Workbook工作薄对象
        Workbook workbook = readExcel("D:\\test.xlsx");
        if (workbook != null) {
            for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
                //只获取sheet1
                if (sheetNum != 0) {
                    continue;
                }
                //获得当前sheet工作表
                Sheet sheet = workbook.getSheetAt(sheetNum);
                if (sheet == null) {
                    continue;
                }
                //循环除了第一行的所有行
                for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                    //获得当前行
                    Row row = sheet.getRow(rowNum);
                    if (row == null) {
                        continue;
                    }
                    //循环当前行
                    String code1 = row.getCell(0).getStringCellValue();
                    String name1 = row.getCell(2).getStringCellValue();

                    Map<String, String> map = query(code1);
                    String code2 = map.get("code");
                    String name2 = map.get("name");

//                    System.out.print(code1 + " ");
//                    System.out.println(name1 + " ");
//                    System.out.println("=====");
//                    System.out.print(code2 + " ");
//                    System.out.println(name2 + " ");

                    if(!name1.equals(name2)){
                        System.out.println("excel -- code:"+code1+",name:"+name1);
                        System.out.println("db    -- code:"+code2+",name:"+name2);
                        System.out.println("=====");
                    }
                    System.out.println();
                }
            }
        }
    }

    //读取excel
    public static Workbook readExcel(String filePath) {
        Workbook wb = null;
        if (filePath == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                return wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                return wb = new XSSFWorkbook(is);
            } else {
                return wb = null;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    // 初始化jdbc
    public static void init() {
        // 不同的数据库有不同的驱动
        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://test1282.db.58dns.org:13310/db58_statusdata?autoReconnect=true";
        String user = "statusdata_user";
        String password = "4fb108037f159ac4";

        try {
            // 加载驱动
            Class.forName(driverName);
            // 设置 配置数据
            // 1.url(数据看服务器的ip地址 数据库服务端口号 数据库实例)
            // 2.user
            // 3.password
            connection = DriverManager.getConnection(url, user, password);
            // 开始连接数据库
            System.out.println("数据库连接成功..");
        } catch (ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

    private static Map<String, String> query(String code) {
        try {
            String sql = "select * from attribution where code = '" + code + "'";
            PreparedStatement pst = connection.prepareStatement(sql);
            ResultSet rSet = pst.executeQuery();

            Map<String, String> map = new HashMap<String, String>(2);
            while (rSet.next()) {
                map.put("code", rSet.getString("code"));
                map.put("name", rSet.getString("name"));
            }
            return map;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
